<html>
<head><meta charset="utf-8"><title>peephole opts in mir · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20opts.20in.20mir.html">peephole opts in mir</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="206251672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20opts%20in%20mir/near/206251672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20opts.20in.20mir.html#206251672">(Aug 07 2020 at 12:40)</a>:</h4>
<p>is there any plan to grow MIR a proper peephole optimization mechanism? </p>
<p>I recently wrote one for LLVM and their API would make something like: <a href="https://github.com/rust-lang/rust/issues/75144">https://github.com/rust-lang/rust/issues/75144</a> trivial to implement.</p>



<a name="206256231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20opts%20in%20mir/near/206256231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20opts.20in.20mir.html#206256231">(Aug 07 2020 at 13:26)</a>:</h4>
<p>I wouldn't know how that would look, can you give an example why that would be trivial with a peephole optimization mechanism?</p>



<a name="206256702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20opts%20in%20mir/near/206256702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20opts.20in.20mir.html#206256702">(Aug 07 2020 at 13:31)</a>:</h4>
<p>a lot of them are modeled as pattern matching + code transformations</p>



<a name="206256762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20opts%20in%20mir/near/206256762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20opts.20in.20mir.html#206256762">(Aug 07 2020 at 13:31)</a>:</h4>
<p>like say, <a href="https://github.com/gcc-mirror/gcc/blob/master/gcc/match.pd#L5266-L5269">https://github.com/gcc-mirror/gcc/blob/master/gcc/match.pd#L5266-L5269</a></p>



<a name="206256849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20opts%20in%20mir/near/206256849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20opts.20in.20mir.html#206256849">(Aug 07 2020 at 13:32)</a>:</h4>
<p>or fitzgen's peepmatic for cranelift <a href="https://github.com/fitzgen/peepmatic#example">https://github.com/fitzgen/peepmatic#example</a></p>



<a name="206414389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20opts%20in%20mir/near/206414389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20opts.20in.20mir.html#206414389">(Aug 09 2020 at 22:28)</a>:</h4>
<p>yea, in rustc it would probably be implemented as a mir traversal which also remembers the shape of previous visited locations</p>



<a name="206414413"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/peephole%20opts%20in%20mir/near/206414413" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Xavier Denis <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/peephole.20opts.20in.20mir.html#206414413">(Aug 09 2020 at 22:29)</a>:</h4>
<p><a href="https://github.com/llvm/llvm-project/blob/master/llvm/lib/Analysis/InstructionSimplify.cpp#L2827">https://github.com/llvm/llvm-project/blob/master/llvm/lib/Analysis/InstructionSimplify.cpp#L2827</a></p>
<p>for example here's the opt i added to llvm to fix an issue with <code>split_at</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>